﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace Chaos.model
{
    public class game : INotifyPropertyChanged, stream.streamable
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void Notify(string property)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        private ObservableCollection<phase.phase> m_phases = new ObservableCollection<phase.phase>();
        public ObservableCollection<phase.phase> Phases
        {
            get { return m_phases; }
            set
            {
                if (m_phases != value)
                {
                    m_phases = value;
                    Notify("Phases");
                }
            }
        }

        
        private ObservableCollection<faction> m_factions = new ObservableCollection<faction>();
        public ObservableCollection<faction> Factions
        {
            get { return m_factions; }
            set
            {
                if (m_factions != value)
                {
                    m_factions = value;
                    Notify("Factions");
                }
            }
        }

        private stream.streamable m_board;
        public board Board
        {
            get { return m_board as board; }
            set
            {
                if (m_board != value)
                {
                    m_board = value;
                    Notify("Board");
                }
            }
        }

        private Enum m_phase = phase.phase.ephase.oldworldphase;
        public phase.phase.ephase Phase
        {
            get { return (phase.phase.ephase)m_phase; }
            set
            {
                if ((phase.phase.ephase)m_phase != value)
                {
                    m_phase = value;
                    Notify("Phase");
                }
            }
        }

        public virtual void serialize(stream.stream io_Stream)
        {
            io_Stream.begin("game")
                .serialize("phase", ref m_phase)
                .serialize("board", ref m_board )
                .serialize("phases", ref m_phases )
                .serialize("factions", ref m_factions)
            .end();
        }

        public faction findfactionbyuser(String in_user)
        {
            for (int i = 0; i < Factions.Count; i++)
            {
                if (Factions[i].User == in_user)
                {
                    return Factions[i];
                }
            }
            return null;
        }

        public faction findfactionbyname(String in_name)
        {
            for (int i = 0; i < Factions.Count; i++)
            {
                if (Factions[i].Name == in_name)
                {
                    return Factions[i];
                }
            }
            return null;
        }
    }
}
